package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.table.AbstractTableModel;

import view.MainFrame;

public class SystemTableModel extends AbstractTableModel {
	private static final long serialVersionUID = 758518191309498940L;
	private ArrayList<Object> data;
	private ArrayList<String> columnNames;

	public SystemTableModel() {
		this.data = new ArrayList<Object>();
		this.columnNames = new ArrayList<String>(Arrays.asList("STT",
				"B\u1ECB kh\u00F3a", "T\u00EAn t\u00E0i kho\u1EA3n",
				"Ch\u1EE7 t\u00E0i kho\u1EA3n",
				"M\u1EADt kh\u1EA9u (M\u00E3 h\u00F3a)", "Quy\u1EC1n h\u1EA1n",
				"L\u1EA7n \u0111\u0103ng nh\u1EADp cu\u1ED1i"));
		loadData(null);
	}

	// TODO load data from database and show in table
	public void loadData(String sql) {
		Connection connect = null;
		try {
			connect = model.Database.connect();
			data.clear();

			PreparedStatement pre = null;
			if (sql == null)
				pre = connect.prepareStatement("SELECT * FROM account");
			else
				pre = connect.prepareStatement(sql);
			ResultSet rs = pre.executeQuery();

			int id;
			boolean lock;
			String name, author, pass, power, lastLoggin;
			while (rs.next()) {
				id = rs.getInt("id");
				lock = rs.getBoolean("lock");
				name = rs.getString("name");
				author = rs.getString("author");
				pass = rs.getString("password");
				power = rs.getString("exe");
				lastLoggin = rs.getString("last_loggin");

				data.add(new Account(id, lock, name, author, pass, power,
						lastLoggin));
			}

			fireTableDataChanged();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (connect != null)
				try {
					connect.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
	}

	public Object getDataRow(int row) {
		try {
			return this.data.get(row);
		} catch (ArrayIndexOutOfBoundsException e) {
			return null;
		}
	}

	public Account getAccountAtRow(int row) {
		return (Account) getDataRow(row);
	}

	@Override
	public boolean isCellEditable(int row, int column) {
		return (column == 1 && !MainFrame.account.isUser()) ? true : false;
	}

	@Override
	public int getRowCount() {
		return this.data.size();
	}

	@Override
	public int getColumnCount() {
		return this.columnNames.size();
	}

	@Override
	public String getColumnName(int column) {
		return this.columnNames.get(column);
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		if (getValueAt(0, columnIndex) != null)
			return getValueAt(0, columnIndex).getClass();
		return String.class;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (!data.isEmpty()) {
			Account acc = (Account) data.get(rowIndex);

			switch (columnIndex) {
			case 0:
				return rowIndex + 1;
			case 1:
				return acc.isLock();
			case 2:
				return acc.getName();
			case 3:
				return acc.getAuthor();
			case 4:
				return acc.getPass();
			case 5:
				return acc.getPower();
			case 6:
				return acc.getLastLoggin();
			}
		}
		return null;
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		Account acc = (Account) data.get(rowIndex);
		switch (columnIndex) {
		case 1:
			acc.setLock((boolean) aValue);
			break;
		case 2:
			acc.setName((String) aValue);
			break;
		case 3:
			acc.setAuthor((String) aValue);
			break;
		case 4:
			acc.setPass((String) aValue);
			break;
		case 5:
			acc.setPower((String) aValue);
			break;
		case 6:
			acc.setLastLoggin((String) aValue);
			break;
		}
		super.setValueAt(aValue, rowIndex, columnIndex);
	}
}
